//选择主键
function getMultiSelect() {
	var row = $("#tblUser").bootstrapTable('getSelections');
	if(row.length > 0) {
		return row[0].suppliecharacterid;

	} else {
		alert("必须要选择一条信息");
		return;
	}
}

$(function() {

	//数据页面加载（主要是表格）
	$.ajax({
		type: "get", //提交类型
		/*dataType: "json", //数据类型*/
		url: "http://localhost:8084/suppliecharacter/user4", //数据访问路径(后端接口)
		data: {
			oper: 5
		},
		success: function(reslt) { //返回成功后所调用得函数（reslt：后端返回的数据）
			var data = JSON.parse(reslt); //将传过来的数据转换为json对象（传进来的参数是json字符串而不是json对象）
			console.log(data.results); //打印后端返回的数据到控制台上
			createTable(data.results); //将返回的数据加载到列表中
		},
		error: function(date) { //返回失败所调用得函数（date：后端返回的数据）
			console.log(date); //打印后端返回的数据
		}
	});
	// 新增按钮点击事件
	$("#btnxinz").click(function() {
		$("#divData").modal();
	});
	//查询按钮点击事件
	$("#btnchaxun").click(function() {
		var name1 = $("#txtcx").val(); //搜取条件从页面中获取的值
		console.log("员工姓名"+name1);
		select2(name1);
		
	});
	//获取输入内容
	$("#btnbaocun").click(function() {

		var name1 = $("#name1").val();
		insert(name1); //添加方法
		$("#divData").modal('hide');

	});
	//删除按钮点击事件
	$("#btndele").click(function() {
		var x = confirm("确定删除？")
		if(x == true) {
			var suppliecharacterid = getMultiSelect();
			console.log(suppliecharacterid);
			DelData("4", suppliecharacterid);
		} else {
			return;
		}

		location.reload();

	});
	
	//修改按钮点击事件
	//修改点击事件
	$("#btnxiugai").click(function() {
		var row = getSelect();
		console.log("获取主键id是：" + row[0].suppliecharacterid);
		$.ajax({
			type: "get",
			url: "http://localhost:8084/suppliecharacter/user4",
			data: {
				oper: 6,
				suppliecharacterid: row[0].suppliecharacterid
			},
			success: function(response) {
				console.log(response);
				var data = JSON.parse(response); //将传过来的数据转换为json对象（传进来的参数是json字符串而不是json对象）
				$.each(data.results, function(indexs, values) {
					console.log(values["name1"] + "---" + indexs);
					$("#suppliecharacterid1").val(values["suppliecharacterid"]);
					$("#name11").val(values["name1"]);
				});

				$("#divData1").modal();
			},
			error: function(response) {
				alert("修改失败");
			}
		});

		/*$("#buildlogid").val(row[0].buildlogid);
		$("#buildingsite").val(row[0].buildingsite);*/
	});
	$("#btnbaocun1").click(function() {

		var suppliecharacterid = $("#suppliecharacterid1").val();
		var name1 = $("#name11").val();
	
		var row = getSelect();
		update(suppliecharacterid, name1);
		$("#divData1").modal('hide');
	});
});	
/*将数据绑定在表格上*/
function createTable(userData) {
	$("#tblUser").bootstrapTable("destroy"); //清空表格
	$("#tblUser").bootstrapTable({
		data: userData,
		loadMsg: "正在加载数据",
		sidePagination: "client",
		pageSize: "4", //分页
		pagination: true,
		/*singleSelect: true,*/
		columns: [{
				field: "state1",
				checkbox: true,
				align: "center"
			},
			{
				field: "name1",
				title: "员工姓名",
				align: "center"
			},
			{
				field: "sex",
				title: "员工性别",
				align: "center"
			},
			{
				field: "phone",
				title: "员工手机号",
				align: "center"
			},
			{
				field: "limit1",
				title: "权限",
				align: "center"
			},
			{
				field: "fromsupplier",
				title: "来自的供应商",
				align: "center"
			},
			{
				field: "managername",
				title: "主管的姓名",
				align: "center"
			},
			{
				field: "state",
				title: "状态（启用或停用）",
				align: "center"
			},
			{
				field: "creater",
				title: "创建人",
				align: "center"
			},
			{
				field: "createtime",
				title: "创建时间",
				align: "center"
			},
			{
				field: "updater",
				title: "更新人",
				align: "center"
			},
			{
				field: "updatetime",
				title: "更新时间",
				align: "center"
			},
			{
				field: "userId",
				title: "账号",
				align: "center"
			},
			{
				field: "password1",
				title: "密码",
				align: "center"
			},
			{
				field: "email",
				title: "邮箱",
				align: "center"
			},
			{
				field: "workplace",
				title: "工作地点",
				align: "center"
			},
			{
				field: "servicearea",
				title: "服务范围",
				align: "center"
			},
			{
				field: "duty",
				title: "职务",
				align: "center"
			},
			{
				field: "educationbackground",
				title: "学历",
				align: "center"
			},
			{
				field: "personid",
				title: "身份证号",
				align: "center"
			},
			{
				field: "remark",
				title: "备注",
				align: "center"
			}
			
		]
	});
}
//添加数据方法
function insert(name1) {
	$.ajax({
		type: "get",
		url: "http://localhost:8084/suppliecharacter/user4",
		data: {
			oper: 2,
			name1: name1
		},
		success: function(response) {
			console.log(response);
			var data = JSON.parse(response);
			alert("添加成功");
			window.location.href = "flgl.html";
		},
		error: function(response) {
			alert("添加失败");
		}
	});

}

//按员工姓名查询
function select2(name1) {
	console.log(name1);
	$.ajax({
		type: "get",
		//				dataTyp: "json", //数据类型
		url: "http://localhost:8084/suppliecharacter/user4",
		data: {
			oper: 1,
			name1: name1
		},
		success: function(response) {
			var data = JSON.parse(response); //将传过来的数据转换为json对象（传进来的参数是json字符串而不是json对象）
			console.log(data.results); //打印后端返回的数据到控制台上
			createTable(data.results); //将返回的数据加载到列表中
		}
	});

}

/*
 删除方法
 * */

function DelData(oper, suppliecharacterid) {
	$.ajax({
		type: "get",
		url: "http://localhost:8084/suppliecharacter/user4",
		data: {
			oper: oper,
			suppliecharacterid: suppliecharacterid
		},
		success: function(response) {
			console.log(response);
			createTable(response);
			alert("删除成功");
		},
		error: function(response) {
			alert("删除成功");
		}
	});
}
//选择修改
function getSelect() {
	//获取列表的行
	var row = $("#tblUser").bootstrapTable('getSelections');
	//alart(row)
	if(row.length > 0) {
		return row;
	} else {
		alert("请选择要修改的数据！");
	}
}
//修改方法
function update(suppliecharacterid, name1) {
	$.ajax({
		type: "get",
		url: "http://localhost:8084/suppliecharacter/user4",
		data: {
			oper: 3,
			suppliecharacterid: suppliecharacterid,
			name1: name1
		},
		success: function(response) {
			console.log(response);//打印返回结果
			
			alert("修改成功");
			window.location.href="flgl.html";
		},
		error: function(response) {
			alert("修改失败");
		}

	});
}